// pages/my/orderDetail/orderDetail.js
import moment from '../../../utils/moment.js'
var app = getApp()
Page({
  data: {
    goodsInfoShow: true,
    goodsPriceShow: false,
    showToast:false,
    showToastTit:'',
    showToastImg:'',
    blackPrice:true,
    logisticsList: [{
      info: "货已到店，本次配送完成，感谢您此次购买，祝您生活愉快！",
      time: "2018-07-28   12:00",
      isNew: true,
      isLast: false
    }],
    addressInfo: {
      title:"收货人",
      name: "",
      phone: "",
      address: ""
    },
    orderDetail: {
      // orderStatus:1,//1待付款 2 待发货 3已发货 4已完成 订单状态
      // goodsPrice:"555",//商品价格
      // freight:"20",//总运费
      // serviceFee:"40",//人工费
      // couponPrice:"50",//优惠价格
      // needPay:"530",//还需支付
      // orderNo:"897365419273",
      // downTime:"2018-07-27 21:00",
      // payTime:"2018-07-27 21:20",
      // deliverTime:"2018-07-28 21:20",
      // payType:"微信扫码支付",
      // goodsList: [{
      //     id:1,
      //     name: '电瓶车电动车控制器新日爱玛雅迪小刀台铃',
      //     url: '../../../image/default_headimg.png',
      //     num: '123',
      //     price: '321',
      //     type: '颜色类别 48V12AH T型通用',
      //     refundStatus: 0,//未产生售后
      //   },
      //   {
      //     id: 2,
      //     name: '电瓶车电动车控制器新日爱玛雅迪小刀台铃',
      //     url: '../../../image/default_headimg.png',
      //     num: '123',
      //     price: '321',
      //     type: '颜色类别 48V12AH T型通用',
      //     refundStatus: 1,//产生售后
      //   },
      // ]
    },
    saler:'',
    isPay:''
  },
  onLoad: function(options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    this.setData({
      orderType: options.type,
      orderId:options.orderId,
      isPay:options.pay?options.pay:'',
      saler:options.saler?options.saler:''
    })
    var orderDetail = this.data.orderDetail;
    orderDetail["orderStatus"] = options.type;
    if (options.type!=0){
      this.setData({
        orderDetail: orderDetail,
        goodsPriceShow: false,
        blackPrice: true,
      })
      this.getDetailByState(options.orderId)
    }else{
      this.setData({
        goodsPriceShow: true,
        blackPrice: false,
      })
      this.getCusDetail(options.orderId)
    }
    
  },
 //配送详情
 logisticsMoretap:function(){
  wx.navigateTo({
    url: '../logistics/logistics',
  })
},
  //删除订单
  deleteOrderTap:function(){
    console.log(1)
    wx.showModal({
      title: '删除订单',
      content: '确定要删除订单吗?',
      cancelColor:"#333333",
      confirmColor: "#539FFF",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //申请退款
  applyRefundTap:function(e){
    let index = e.currentTarget.dataset.index
    let data = {
      orderId:this.data.orderId,
      orderNo:this.data.orderDetail.orderNo,
      manufacturerId:this.data.orderDetail.manufacturerId,
      productDetail:this.data.orderDetail.goodsList[index]
    }
    let productInfo = JSON.stringify(data)
    console.log(productInfo)
    wx.navigateTo({
      url: '../applyRefund/applyRefund?info='+productInfo
    })
  },
  //催单
  reminderTap:function(e){
    var that = this
    let time = this.data.orderDetail.downTime
    console.log(time)
    let begin = moment(time).add(3,'d')
    console.log(begin)
    let now = moment()
    if (moment().isAfter(begin)) {  //三天后
      console.log(123)
      let data = {
        orderId:this.data.orderId
      }
      console.log(data)
      app.request('wx_app/agent_order/sendReminder',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          this.setData({
            showToast:true,
            showToastTit:'已催促厂家发货',
            showToastImg:'../../../image/success.png'
          })
          setTimeout(function(){
            that.setData({
              showToast:false
            })
          },1500)
        }else{
          app.errorMsg(res.message)
        }
      })
    }else{  //没到三天
      console.log(321)
      this.setData({
        showToast:true,
        showToastTit:'下单后3日才可催单',
        showToastImg:'../../../image/wrong.png'
      })
      setTimeout(function(){
        that.setData({
          showToast:false
        })
      },1500)
    }
  },
  orderDetailTap:function(event){
    var id=event.currentTarget.dataset.goodId;
    var type = event.currentTarget.dataset.type;
    if (this.data.orderType>0) {    
      wx.navigateTo({
        url: '../../my/purchase/purchaseDetail?id='+id+'&goodsType='+type,
      })
    }else{
      wx.navigateTo({
        url: '../../stock/goodsDetail?id='+id+'&goodsType='+type,
      })
    }
  },
  //客户订单详情
  getCusDetail(orderId){
    let data = {
      orderId
    }
    let goodsList = []
    app.request('wx_app/user_order/detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        res.data.orderDetails.forEach(item=>{
          goodsList.push({
            name:item.name,
            url: item.pic,
            num: item.amount,
            price: item.price,
            type: item.des,
            productId:item.productId,
            productType:item.productType,
          })
        })
        this.setData({
          ['orderDetail.goodsPrice']:res.data.orderPrice,
          ['orderDetail.orderNo']:res.data.orderNo,
          ['orderDetail.downTime']:res.data.createTime.split('T')[0],
          ['orderDetail.payWay']:res.data.payWay,
          ['orderDetail.goodsList']:goodsList,
        })
      }
    })
  },
  //待发货，已发货、待付款订单详情
  getDetailByState(orderId){
    let data = {
      orderId
    }
    let goodsList = []
    console.log(data)
    app.request('wx_app/agent_order/detailByState',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.orderDetails.forEach(item=>{
          goodsList.push({
            name: item.name,
            url: item.pic,
            num: item.amount,
            price: item.price,
            type: item.versionValueDesc,
            orderDetailId:item.orderDetailId,
            productId:item.productId,
            productType:item.productType,
            refundStatus: item.isApplyReturns==1?1:0
          })
        })
        this.setData({
          ['addressInfo.name']:res.data.consignee,
          ['addressInfo.address']:res.data.address,
          ['addressInfo.phone']:res.data.consignee_phone,
          ['orderDetail.orderNo']:res.data.orderNo,
          ['orderDetail.goodsPrice']:res.data.orderPrice,
          ['orderDetail.downTime']:res.data.createTime.split('T')[0],
          ['orderDetail.logisticsType']:res.data.logisticsType,
          ['orderDetail.goodsList']:goodsList,
          ['orderDetail.freight']:res.data.deliveryFee,
          ['orderDetail.deliveryTime']:res.data.deliveryTime,
          ['orderDetail.manufacturerId']:res.data.manufacturerId
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //取消订单
  cancelOrder(){
    let data = {
      orderId:this.data.orderId
    }
    app.request('wx_app/agent_order/cancelWaitDeliverOrder',data,'GET').then(res=>{
      if (res.code==1) {
        wx.showToast({
          title:'删除成功',
          success(){
            setTimeout(function(){
              wx.switchTab({
                url:'../my'
              })
            },1000)
          }
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //删除订单
  deletOrder(){
    let data = {
      orderId:this.data.orderId
    }
    app.request('wx_app/agent_order/del',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showModal({
          title:'删除订单',
          content:'确定要删除订单吗？',
          success: function(res) {
            if (res.confirm) {
              wx.showToast({
                title:'删除成功',
                success(){
                  setTimeout(function(){
                    wx.switchTab({
                      url:'../my'
                    })
                  },1000)
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})